package com.lelefly.base.controller;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lelefly.base.model.AddFlyerBean;
import com.lelefly.base.model.AddOrganizationBean;
import com.lelefly.base.model.Page;
import com.lelefly.base.model.Result;
import com.lelefly.base.po.LeleflyOrganization;
import com.lelefly.base.po.LeleflyOrganizationInfo;
import com.lelefly.base.po.LeleflyUser;
import com.lelefly.base.serviceImp.LeleflyFlyerService;
import com.lelefly.base.utils.AuthUtils;
import com.lelefly.constant.Constant;
import com.lelefly.handleexecption.ServiceExecption;

/**
 * 飞手管理控制类
 *
 */
@Controller
@RequestMapping("/flyer")
public class LeleFlyFlyerController {

	@Resource
	private LeleflyFlyerService flyerService;
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	/**
	 * 添加飞手
	 * @return
	 */
	@RequestMapping(value="/add_flyer",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<LeleflyOrganization> addFlyer(AddFlyerBean flyerBean){
		Result<LeleflyOrganization> result = new Result<>();
		
		if (flyerBean == null) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("飞手信息为空");
			return result;
		}
		if (StringUtils.isEmpty(flyerBean.getFlyerName())
				|| StringUtils.isEmpty(flyerBean.getFlyerPhone())
				|| StringUtils.isEmpty(flyerBean.getOwnerPhone())
				) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("必输项为空");
			return result;
		}
		
		if (!AuthUtils.isAuthed(jdbcTemplate, flyerBean.getFlyerPhone(), "2")) {
			
			result.setMes("当前角色无操作权限");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		result = flyerService.addOrganizationInfo(flyerBean.getOwnerPhone(), 
				flyerBean.getFlyerPhone(), 
				flyerBean.getFlyerName(),
				flyerBean.getCode());
		return result;
	}
	
	/**
	 * 移除该组织中的飞防队成员
	 * @param flyerBean
	 * @return
	 */
	@RequestMapping(value="/remove_flyer",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<LeleflyOrganization> removeFlyer(AddFlyerBean flyerBean){
		Result<LeleflyOrganization> result = new Result<>();
		if (flyerBean == null) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("飞手信息为空");
			return result;
		}
		if (StringUtils.isEmpty(flyerBean.getFlyerPhone())
				|| StringUtils.isEmpty(flyerBean.getOwnerPhone())
				) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("必输项为空");
			return result;
		}
		
		result = flyerService.removeFlyer(flyerBean);
		return result;
	}
	
	/**
	 * 查询当前手机号码下的飞防队成员
	 * @param phone
	 * @return
	 */
	@RequestMapping(value="/query_flyer",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<LeleflyOrganizationInfo> queryFlyer(@RequestBody String phone){
		Result<LeleflyOrganizationInfo> result = new Result<>();
		if (StringUtils.isEmpty(phone)) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("手机号码不能为空");
			return result;
		}
		
		result = flyerService.queryOrganization(phone);
		
		return result;
	}
	
	/**
	 * 分页查询所有飞防组织
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/query_all_organization",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<LeleflyOrganization> queryOrganization(Page page){
		Result<LeleflyOrganization> result = new Result<>();
		
		result = flyerService.queryOrganization(page);
		return result;
	}
	
	/**
	 * 创建飞防组织
	 * @param organizationBean
	 * @return
	 */
	@RequestMapping(value="/create_organization",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<LeleflyOrganization> createOrganization(AddOrganizationBean organizationBean){
		Result<LeleflyOrganization> result = new Result<>();
		
		if (organizationBean == null
				|| StringUtils.isEmpty(organizationBean.getOwnerPhone())
				|| StringUtils.isEmpty(organizationBean.getOrganizationName())) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("参数不能为空");
			return result;
		}
		
		result = flyerService.createOrganization(organizationBean);
		return result;
	}
	
	/**
	 * 向飞手发送邀请短信
	 * @return
	 * @param phoneNum
	 * 飞手手机号码
	 */
	public Result sendInvitationMessage(String ownerPhone,String phoneNum){
		Result result = new Result();
		if (StringUtils.isEmpty(phoneNum)) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("手机号码为空");
			return result;
		}
		
//		LeleflyUser leleflyUser =(LeleflyUser) flyerService.getUser(phoneNum);
//		if (leleflyUser == null) {
//			result.setState(Constant.HTTP_STATUS_FAIL);
//			result.setMes("该用户不存在");
//			return result;
//		}
		result = flyerService.sendInvitationMessage(ownerPhone,phoneNum);
		return result;
	}
	
	
	public Result addOrganizationByNum(){
		return null;
	}
}
